package com.sevntu.apvs.lab1;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

public class ObtainFunction {

	int numberOfGraphArcN;
	int nodeCount;
	int arcsCount;
	byte[] modelRAM;
	byte[] resultArray;

	public ObtainFunction(int numberOfGraphArcN, int nodeCount, int arcsCount, byte[] modelRAM) {
		this.numberOfGraphArcN = numberOfGraphArcN;
		this.nodeCount = nodeCount;
		this.arcsCount = arcsCount;
		this.modelRAM = modelRAM;
	}

	public byte[] getResult() {

		int offset = 0;
		int[] nodeArray = new int[nodeCount];
		byte memCode = 0;
		// bytes number for one node 
		int nodeSize = new BigDecimal(arcsCount * 2 / 8).setScale(0, RoundingMode.UP).intValue();

		for (int currentArcs = 0; currentArcs < arcsCount; currentArcs++) {
			for (int currentNode = 0; currentNode < nodeCount; currentNode++) {
				offset = currentArcs * nodeSize * 8 + 2 * currentNode;
				memCode = (byte) (modelRAM[offset] << 1 | modelRAM[offset + 1]);
				if (memCode == 2 || memCode == 1)
					nodeArray[currentNode]++;
			}
		}

		ArrayList<Byte> result = new ArrayList<Byte>();
		for (int i = 0; i < nodeArray.length; i++)
			if (nodeArray[i] > numberOfGraphArcN)
				result.add((byte) (i + 1));

		resultArray = new byte[result.size()];
		int count = 0;
		for (byte copy : result) {
			resultArray[count] = copy;
			count++;
		}
		return resultArray;
	}

	public int getSum() {
		return resultArray.length;
	}
}
